package com.yygo.util;

import com.yygo.configentity.action.Action;
import com.yygo.configentity.action.Role;
import com.yygo.configentity.action.Phase;
import com.yygo.configentity.action.RoleActions;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class RoleActionConfigUtil {
    private static Log log = LogFactory.getLog(RoleActionConfigUtil.class);
    public RoleActions read(){
        RoleActions roleActions = new RoleActions();
        SAXReader saxReader= new SAXReader();
        Document document = null;
        try{
            URL xmlPath = this.getClass().getClassLoader().getResource("RoleActions.xml");
            document = saxReader.read(xmlPath);
            List childNodes = document.selectNodes("//RoleActions/Role" );
            List<Role> roles = new ArrayList<>();
            for (Object obj:childNodes) {
                Role role = new Role();
                Element childNode = (Element)obj;
                String name = childNode.valueOf("@name" );
                role.setName(name);
                List<Phase> roleActionList = new ArrayList<>();
                for(Object object:childNode.elements("Phase")){
                    Phase roleAction = new Phase();
                    Element properties = (Element)object;
                    String phaseName =  ((Node)properties).valueOf("@name" );
                    boolean grab = ((Node) properties).valueOf("@grab").equals("true")? true:false;
                    boolean canSave = ((Node) properties).valueOf("@canSave").equals("true")? true:false;
                    boolean needDealerComment = ((Node)properties).valueOf("@needDealerComment").equals("true")? true:false;
                    boolean needAuditComment = ((Node)properties).valueOf("@needAuditComment").equals("true")? true:false;
                    String dealerCommentTitle =  ((Node)properties).valueOf("@dealerCommentTitle").replace("\n", "").replace(" ", "");
                    String auditCommentTitle =  ((Node)properties).valueOf("@auditCommentTitle").replace("\n", "").replace(" ", "");
                    roleAction.setName(phaseName);
                    roleAction.setGrab(grab);
                    roleAction.setCanSave(canSave);
                    roleAction.setNeedAuditComment(needAuditComment);
                    roleAction.setNeedDealerComment(needDealerComment);
                    roleAction.setAuditCommentTitle(auditCommentTitle);
                    roleAction.setDealerCommentTitle(dealerCommentTitle);
                    List<Action> actions = new ArrayList<>();
                    Element actionEle = properties.element("Actions");
                    for(Object actionObj : actionEle.elements("Action")) {
                        Action action = new Action();
                        Element actionNode = (Element) actionObj;
                        String actionName = ((Node)actionNode).valueOf("@name").replace("\n", "").replace(" ", "");
                        String actionLabel = ((Node)actionNode).valueOf("@label").replace("\n", "").replace(" ", "");
                        String actionToStatus = ((Node)actionNode).valueOf("@toStatus").replace("\n", "").replace(" ", "");
                        String actionToRole = ((Node)actionNode).valueOf("@toRole").replace("\n", "").replace(" ", "");
                        String autoAction = ((Node)actionNode).valueOf("@autoAction").replace("\n", "").replace(" ", "");
                        boolean isSlave = ((Node)actionNode).valueOf("@isSlave").equals("true")? true:false;
                        boolean isMaster = ((Node)actionNode).valueOf("@isMaster").equals("true")? true:false;
                        String actionConfirmMsg = ((Node)actionNode).valueOf("@confirmMsg").replace("\n", "").replace(" ", "");
                        String actionToPhase = ((Node)actionNode).valueOf("@toPhase").replace("\n", "").replace(" ", "");
                        String actionStyle = ((Node)actionNode).valueOf("@buttonSytle").replace("\n", "").replace(" ", "");
                        String checkBackType = ((Node)actionNode).valueOf("@checkBackType").replace("\n", "").replace(" ", "");
                        boolean sendback = ((Node)actionNode).valueOf("@sendback").equals("true")? true:false;
                        boolean sendforward = ((Node)actionNode).valueOf("@sendforward").equals("true")? true:false;
                        action.setName(actionName);
                        action.setConfirmMsg(actionConfirmMsg);
                        action.setLabel(actionLabel);
                        action.setToPhase(actionToPhase);
                        action.setToStatus(actionToStatus);
                        action.setToRole(actionToRole);
                        action.setAutoAction(autoAction);
                        action.setButtonStyle(actionStyle);
                        action.setIsMaster(isMaster);
                        action.setIsSlave(isSlave);
                        action.setSendback(sendback);
                        action.setSendforward(sendforward);
                        action.setFromRole(name);
                        action.setCheckBackType(checkBackType);
                        actions.add(action);
                    }
                    roleAction.setActions(actions);
                    roleAction.setRole(role.getName());
                    roleActionList.add(roleAction);
                }
                role.setRoleActions(roleActionList);
                roles.add(role);
            }
            roleActions.setRoles(roles);
        }
        catch(Exception e){
            log.trace(e.getMessage());
        }
        return roleActions;
    }
}
